package com.metacube.questionbank.dao;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

/*
 * @author Team Neeraj Jain
 * */
@Repository
public class VoteDAO {
	@Autowired
	DataSource dataSource;
	@Autowired
	JdbcTemplate jdbcTemplate;

	@PostConstruct
	public void setJdbcTemplate() {
		jdbcTemplate.setDataSource(dataSource);
	}

	/*
	 * @param answerId id of answer This method returns no of likes for a
	 * particular answer
	 * 
	 * @return nLikes no. of likes
	 */
	public Integer getUpvotesForAnswer(int answerId) {
		String sql = "select count(upvote_status) from votes where answer_id="
				+ answerId + " and upvote_status=1";
		int count;
		count = jdbcTemplate.queryForInt(sql);
		return count;
	}

	/*
	 * @param answerId id of answer This method returns no of dislikes for a
	 * particular answer
	 * 
	 * @return nLikes no. of dislikes
	 */
	public int getdownvotesForAnswer(int answerId) {
		String sql = "select count(upvote_status) from votes where answer_id="
				+ answerId + " and upvote_status=-1";
		int count;
		count = jdbcTemplate.queryForInt(sql);
		return count;
	}

	/*
	 * @param userId id of user
	 * 
	 * @param ansId id of answer This method checks whether a user has already
	 * liked or disliked a particular answer
	 * 
	 * @return true or false
	 */
	public int isRecordPresent(int userId, int ansId) {
		String sql = "select count(*) from votes where user_id=" + userId
				+ " and answer_id=" + ansId;
		int statusStored = jdbcTemplate.queryForInt(sql);
		return statusStored;
	}

	/*
	 * @param userId id of user
	 * 
	 * @param ansId id of answer
	 * 
	 * @param status like or dislike This method stores the like or dislike of
	 * answer in the database.
	 */
	public void insertStatus(int userId, int ansId, int status) {
		String sql = "INSERT INTO votes (answer_id,user_id,upvote_status) VALUES (?,?,?)";
		JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
		jdbcTemplate.update(sql, new Object[] { ansId, userId, status });
	}

	/*
	 * @param userId id of user
	 * 
	 * @param ansId id of answer This method checks whether a user has liked or
	 * disliked a particular answer
	 * 
	 * @return like(1) or dislike(-1)
	 */
	public int getUpvoteStatus(int userId, int ansId) {
		String sql = "select upvote_status from votes where user_id=" + userId
				+ " and answer_id=" + ansId;
		int statusStored = jdbcTemplate.queryForInt(sql);
		return statusStored;
	}

	/*
	 * @param userId id of user
	 * 
	 * @param ansId id of answer This method updates the like or dislike for a
	 * particular answer by a user.
	 */
	public void updateStatus(int userId, int ansId, int status) {
		String sql = "Update votes set upvote_status=" + status
				+ " where user_id=" + userId + " and answer_id=" + ansId;
		jdbcTemplate.update(sql);

	}

	/*
	 * @param userId This method finds the total no of likes for the answers of
	 * a particular user.
	 * 
	 * @return no of likes for answers of user
	 */
	public int getCountUpvotesForUser(int userId) {
		String sql = "select count(upvote_status) from votes join answers where answers.answer_id=votes.answer_id and answers.user_id="
				+ userId + " and upvote_status=1";
		int count;
		count = jdbcTemplate.queryForInt(sql);
		return count;
	}

	/*
	 * @param userId This method finds the total no of dislikes for the answers
	 * of a particular user.
	 * 
	 * @return no of dislikes for answers of user
	 */
	public int getCountDownUpvotesForUser(int userId) {
		String sql = "select count(upvote_status) from votes join answers where answers.answer_id=votes.answer_id and answers.user_id="
				+ userId + " and upvote_status=-1";
		int count;
		count = jdbcTemplate.queryForInt(sql);
		return count;
	}
}